<?php
namespace app\admin\controller;
use think\Controller;
use caretree\Caretree;  //无线级分类  在extend文件夹下新建一个Caretree文件在创建一个Caretree.php 命名空间为 包名
class Cate extends Controller 
{
	
    public function lst()
    {
		$cate = new Caretree();
		$cateObj= db('cate');
		if(request()->isPost()){
			$data = input('post.');
			
			// dump($data);
			// die;
			$cate->cateSort($data['sort'],$cateObj);
			$this->success('排序成功',url('lst'));
		}
		$cateRes = $cateObj->order('sort DESC')->select();
		$cateRes = $cate->Caretree($cateRes);
		$this->assign([
			'cateRes' => $cateRes
		]);
		return view('list');
	}
	public function add()
	{
	    $cate=new Caretree();
	    $cateObj=db('cate');
		if(request()->isPost()){
			$data=input('post.');
	        //判断是否可以添加子分类
	        if(in_array($data['pid'], ['1','3'])){
	            $this->error('系统分类不能作为上级分类！');
	        }
	        //
	        if($data['pid']==2){
	            $data['cate_type']=3;
	        }
	        //
	        $cateId=$cateObj->field('pid')->find($data['pid']);
	        $cateId=$cateId['pid'];
	        if($cateId==2){
	            $this->error('此分类不能作为上级分类！');
	        }
			//验证
			$validate = validate('Cate');
			if(!$validate->check($data)){
			    $this->error($validate->getError());
			}
			$add=$cateObj->insert($data);
			if($add){
				$this->success('添加分类成功！','lst');
			}else{
				$this->error('添加分类失败！');
			}
			return;
		}
	    $cateRes=$cateObj->order('sort DESC')->select();
	    $cateRes=$cate->caretree($cateRes);
	    $this->assign([
	        'cateRes'=>$cateRes,
	        ]);
	    return view();
	}
	public function edit()
	{
		$cate = new Caretree();
		$cateObj= db('cate');
		if(request()->isPost()){
			$data = input('post.');
			
			
			
			//验证
    		$validate = validate('Cate');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}
			$save= $cateObj->update($data);
			if($save !== false){
				$this->success('修改分类成功!','lst');
			}else{
				$this->error('修改分类失败');
			}
			return;
		}
		$cates = $cateObj->find(input('id'));
		$cateRes = $cateObj->order('sort DESC')->select();
		$cateRes = $cate ->Caretree($cateRes);
		$this->assign([
			'cateRes' => $cateRes,
			'cates' => $cates
		]);
		return view();
	}
	public function del($id)
	{	$cate = db('cate');
		$Caretree = new Caretree();
		$sonids = $Caretree->childrenids($id,$cate);
		//dump($sonids);
		$sonids[] = intval($id); //新增一个整型值
		$arrSys = [1,2,3];
		$arrRes = array_intersect($arrSys,$sonids);
		if($arrRes){
			$this->error('系统内置文章分类不允许删除!');
		}
		//删除分类前判断该分类下的文章和文章和相关的缩略图 
		$article = db('article');
		foreach ($sonids as $k => $v){

			//通过tp_cate的id字段  查询article表中的id和缩略图字段
			$artRes = $article->field('id,thumb')->where(array('cate_id'=>$v))->select();
			foreach ($artRes as $k1 => $v1){
				//遍历字段thumb拼接图片的绝对路径
				$thumbSrc = IMG_UPLOADS.$v1['thumb'];
				if(file_exists($thumbSrc)){
					@unlink($thumbSrc);
				}
				//通过id字段删除该文章
				$article->delete($v1['id']);
			}
		}
		$del=$cate->delete($sonids);
    	if($del){
			$this->success('删除分类成功！','lst');
		}else{
			$this->error('删除分类失败！');
		}
		return view();
	}
	
}
